package org.sippr.rvt2ifc.controller;

import org.sippr.rvt2ifc.pojo.*;
import org.sippr.rvt2ifc.service.ProjectModelRelationService;
import org.sippr.rvt2ifc.service.ProjectService;
import org.sippr.rvt2ifc.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/project")

public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectModelRelationService projectModelRelationService;
    // 用于存储客户端请求
    private final ConcurrentHashMap<String, DeferredResult<Result>> clients = new ConcurrentHashMap<>();

    // 超时时间 (300秒)
    private static final long TIMEOUT = 300_000L;
    /*
     * 获取所有轻量化
     *
     * @param null
     * @return
     * @author xuegang
     * @create 2025/1/23
     **/

    @GetMapping("/list")
    public Result list() {
        return Result.success(projectService.list());
    }

    /*
     * 新增项目
     *
     * @param null
     * @return
     * @author xuegang
     * @create 2025/1/23
     **/

    @PostMapping("/insert")
    public Result insert(@RequestBody Project project) {
        if (project.getModelUuids().stream().count() < 1)
            return Result.error("请选择模型");
        project.setState((short) 0);
        project.setUuid(SnowflakeIdGenerator.nextId());
        projectService.insert(project);

        for (String modelUuid : project.getModelUuids()) {
            ProjectModelRelation projectModelRelation = new ProjectModelRelation();
            projectModelRelation.setProjectUuid(project.getUuid());
            projectModelRelation.setXktModelUuid(modelUuid);
            projectModelRelationService.insert(projectModelRelation);
        }

        List<Project> waitProjectList = projectService.getWaitProject();

        // 如果有新数据，通知所有客户端
        if (!waitProjectList.isEmpty()) {
            for (DeferredResult<Result> deferredResult : clients.values()) {
                deferredResult.setResult(Result.success(waitProjectList));
            }
        }
        project = projectService.selectByUuid(project.getUuid());
        return Result.success(project);

    }

    @GetMapping("/getWaitList")
    public DeferredResult<Result> getWaitList() {
        // 创建 DeferredResult，超时时间为30秒
        DeferredResult<Result> deferredResult = new DeferredResult<>(TIMEOUT, Result.success("No updates"));

        // 处理完成或超时逻辑
        deferredResult.onTimeout(() -> clients.remove(deferredResult.toString()));
        deferredResult.onCompletion(() -> clients.remove(deferredResult.toString()));

        // 将请求存储到集合中
        clients.put(deferredResult.toString(), deferredResult);

        // 检查是否有新的数据
        List<Project> waitProjectList = projectService.getWaitProject();
        if (!waitProjectList.isEmpty()) {
            deferredResult.setResult(Result.success(waitProjectList)); // 立即返回数据
        }

        return deferredResult;
    }

    @PostMapping("/updateState")
    public Result updateState(String projectUuid, Short state) {
        projectService.updateState(projectUuid, state);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> uuids) {


        projectService.delete(uuids);
        return Result.success();
    }

}
